There's more on GitHub. Find the complete example and learn how to set up and run in theAWS CodeExamples Repository.
Call functions that transfer files to and from an S3 bucket using the S3TransferManager.
public Integer downloadObjectsToDirectory(S3TransferManager transferManager,URI destinationPathURI, String bucketName) {DirectoryDownload directoryDownload = transferManager.downloadDirectory(DownloadDirectoryRequest.builder().destination(Paths.get(destinationPathURI)).bucket(bucketName).build());CompletedDirectoryDownload completedDirectoryDownload = directoryDownload.completionFuture().join();completedDirectoryDownload.failedTransfers().forEach(fail -> logger.warn("Object [{}] failed to transfer", fail.toString()));return completedDirectoryDownload.failedTransfers().size();}Upload an entire local directory.
public Integer uploadDirectory(S3TransferManager transferManager,URI sourceDirectory, String bucketName) {DirectoryUpload directoryUpload = transferManager.uploadDirectory(UploadDirectoryRequest.builder().source(Paths.get(sourceDirectory)).bucket(bucketName).build());CompletedDirectoryUpload completedDirectoryUpload = directoryUpload.completionFuture().join();completedDirectoryUpload.failedTransfers().forEach(fail -> logger.warn("Object [{}] failed to transfer", fail.toString()));return completedDirectoryUpload.failedTransfers().size();}Upload a single file.
public String uploadFile(S3TransferManager transferManager, String bucketName, String key, URI filePathURI) {UploadFileRequest uploadFileRequest = UploadFileRequest.builder().putObjectRequest(b -> b.bucket(bucketName).key(key)).source(Paths.get(filePathURI)).build();FileUpload fileUpload = transferManager.uploadFile(uploadFileRequest);CompletedFileUpload uploadResult = fileUpload.completionFuture().join();return uploadResult.response().eTag();}The code examples use the following imports.
import org.slf4j.Logger;import org.slf4j.LoggerFactory;import software.amazon.awssdk.core.exception.SdkException;import software.amazon.awssdk.core.sync.RequestBody;import software.amazon.awssdk.services.s3.S3AsyncClient;import software.amazon.awssdk.services.s3.S3Client;import software.amazon.awssdk.services.s3.model.CompletedMultipartUpload;import software.amazon.awssdk.services.s3.model.CompletedPart;import software.amazon.awssdk.services.s3.model.CreateMultipartUploadResponse;import software.amazon.awssdk.services.s3.model.PutObjectResponse;import software.amazon.awssdk.services.s3.model.UploadPartRequest;import software.amazon.awssdk.services.s3.model.UploadPartResponse;import software.amazon.awssdk.services.s3.waiters.S3Waiter;import software.amazon.awssdk.transfer.s3.S3TransferManager;import software.amazon.awssdk.transfer.s3.model.FileUpload;import software.amazon.awssdk.transfer.s3.model.UploadFileRequest;import java.io.IOException;import java.io.RandomAccessFile;import java.net.URISyntaxException;import java.net.URL;import java.nio.ByteBuffer;import java.nio.file.Paths;import java.util.ArrayList;import java.util.List;import java.util.Objects;import java.util.UUID;import java.util.concurrent.CompletableFuture;Use the S3 Transfer Manager on top of the AWS CRT-based S3 client to transparently perform a multipart upload when the size of the content exceeds a threshold. The default threshold size is 8 MB.
/** * Uploads a file to an Amazon S3 bucket using the S3TransferManager. * * @param filePath the file path of the file to be uploaded */public void multipartUploadWithTransferManager(String filePath) {S3TransferManager transferManager = S3TransferManager.create();UploadFileRequest uploadFileRequest = UploadFileRequest.builder().putObjectRequest(b -> b.bucket(bucketName).key(key)).source(Paths.get(filePath)).build();FileUpload fileUpload = transferManager.uploadFile(uploadFileRequest);fileUpload.completionFuture().join();transferManager.close();}Use the S3Client API to perform a multipart upload.
/** * Performs a multipart upload to Amazon S3 using the provided S3 client. * * @param filePath the path to the file to be uploaded */public void multipartUploadWithS3Client(String filePath) {// Initiate the multipart upload.CreateMultipartUploadResponse createMultipartUploadResponse = s3Client.createMultipartUpload(b -> b.bucket(bucketName).key(key));String uploadId = createMultipartUploadResponse.uploadId();// Upload the parts of the file.int partNumber = 1;List completedParts = new ArrayList();ByteBuffer bb = ByteBuffer.allocate(1024 * 1024 * 5); // 5 MB byte buffertry (RandomAccessFile file = new RandomAccessFile(filePath, "r")) {long fileSize = file.length();long position = 0;while (position < fileSize) {file.seek(position);long read = file.getChannel().read(bb);bb.flip(); // Swap position and limit before reading from the buffer.UploadPartRequest uploadPartRequest = UploadPartRequest.builder().bucket(bucketName).key(key).uploadId(uploadId).partNumber(partNumber).build();UploadPartResponse partResponse = s3Client.uploadPart(uploadPartRequest,RequestBody.fromByteBuffer(bb));CompletedPart part = CompletedPart.builder().partNumber(partNumber).eTag(partResponse.eTag()).build();completedParts.add(part);bb.clear();position += read;partNumber++;}} catch (IOException e) {logger.error(e.getMessage());}// Complete the multipart upload.s3Client.completeMultipartUpload(b -> b.bucket(bucketName).key(key).uploadId(uploadId).multipartUpload(CompletedMultipartUpload.builder().parts(completedParts).build()));}Use the S3AsyncClient API with multipart support enabled to perform a multipart upload.
/** * Uploads a file to an S3 bucket using the S3AsyncClient and enabling multipart support. * * @param filePath the local file path of the file to be uploaded */public void multipartUploadWithS3AsyncClient(String filePath) {// Enable multipart support.S3AsyncClient s3AsyncClient = S3AsyncClient.builder().multipartEnabled(true).build();CompletableFuture response = s3AsyncClient.putObject(b -> b.bucket(bucketName).key(key),Paths.get(filePath));response.join();logger.info("File uploaded in multiple 8 MiB parts using S3AsyncClient.");}